package com.yicong.ants.ui.video;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseStatefulActivity;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.video.GiftLog;
import com.yicong.ants.databinding.ActivityMyGiftListBinding;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.utils.StatusBarUtil;

import java.util.HashMap;
import java.util.Map;

public class MyGiftListActivity extends BaseStatefulActivity<ActivityMyGiftListBinding> implements View.OnClickListener {
    private RecyclerView rv;
    StatefulBindQuickAdapter<GiftLog> mGiftAdapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_my_gift_list;
    }

    @Override
    protected void initEventAndData() {
        StatusBarUtil.setStatusImmerAndTextWhite(getWindow());
        mDataBind.topField.setPadding(UiHelper.dp2px(15), StatusBarUtil.getStatusBarHeight(mContext), UiHelper.dp2px(15), UiHelper.dp2px(15));
        mDataBind.tvName.setText("礼物记录");
        mDataBind.setClick(this);
        initAdapter();
    }

    private void initAdapter() {
        rv = mDataBind.rv;
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        mGiftAdapter = new StatefulBindQuickAdapter<GiftLog>(R.layout.item_my_gift) {
            @Override
            protected void convert(DataBindViewHolder helper, GiftLog item) {
                helper.getBinding().setVariable(BR.bean, item);
            }

            @Override
            public void loadPageData(int page) {
                final Map<String, String> params = new HashMap<>();
                params.put("page", String.valueOf(page));
                params.put("page_size", String.valueOf(Constants.PAGE_SIZE));

                if (page == 1) {
                    mGiftAdapter.setViewState(MultiStateView.VIEW_STATE_LOADING);
                }

                addSubscribe(RetrofitHelper.getApis()
                        .giftLog(params)
                        .compose(RxHelper.toMain())
                        .subscribe(respBean -> {
                            if (respBean.isCodeFail()) {
                                showToast(respBean.getMsg());
                                if (page == 1) {
                                    setViewState(MultiStateView.VIEW_STATE_ERROR);
                                }
                                return;
                            }
                            solveData(respBean.getData(), page, Constants.PAGE_SIZE);
                        }, RxHelper.getSwitchableErrorConsumer(this)));
            }
        };
        mGiftAdapter.bindToRecyclerView(rv);
        rv.setAdapter(mGiftAdapter);
        mGiftAdapter.loadPageData(1);
    }

    @Override
    protected void onLoadData() {
    }

    @Override
    public void onClick(View v) {
        onBackPressed();
    }
}